package Lenguaje;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import vo.Clase;
import vo.Programa;

public class Driver {
	
	String[] my_args;
	public static void main(String[] args) {
		Driver d = new Driver(args);
		d.run(); 
	}
	
	public Driver(String[] args) {
		my_args = args; 
	}
	
	private void run() {
		try{
			
			if(my_args.length>0){
				FileReader fr = new FileReader(new File(my_args[0]));
				Lexer l = new Lexer(fr);
				Programa programa=new Programa();
				ArrayList clases=new ArrayList();
				Token s = null ;
				
				try {
					do{
						s = l.nextToken();
						if(s.thislabel==Token.Label.RESERVADA && s.thisval.equals("class")){
							
							
							//System.out.println(s.thisval);
							Clase clase =this.EstructuraClase(l);
							//clases.add(clase);
						}
												
					} while (s.thislabel != Token.Label.EOF);
					/*
					programa.setIfElse(ifElses);
					//Enlace con el generador
					Generador generador=new Generador();
					generador.run(programa);
					 */
					
					System.out.println("\n\nScanner done - Words processed = " + l.no_of_words);
				} catch (IOException e) {
					e.printStackTrace();
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			else{
				System.out.println("Debe pasar un archivo por parametro");
			}
			
			
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		  }		 
		
		
	}

	private Clase EstructuraClase(Lexer l) {
		// TODO Auto-generated method stub
		Token s = null ;
		Clase clase = new Clase();
		String visibilidad="",nombre="",tipo="";
		
		try {
			s = l.nextToken();
			if(s.thislabel==Token.Label.IDENTIFICADOR ){
				clase.setNombre((String)s.thisval);
				s = l.nextToken();
				
				if (s.thislabel==Token.Label.SIMBOLO && s.thisval.equals("{"))
				{
					System.out.println("{");
					s = l.nextToken();
					
					while (s.thislabel==Token.Label.RESERVADA && s.thisval.equals("attribute"))
					{
						
						s = l.nextToken();
						
						if (s.thislabel==Token.Label.SIMBOLO && s.thisval.equals("(")) {
							System.out.println("(");
							
							s = l.nextToken();
							
							if(s.thislabel==Token.Label.SIMBOLO && (s.thisval.equals("-")||s.thisval.equals("+")||s.thisval.equals("#")) ){								

								
								if (s.thislabel==Token.Label.SIMBOLO && s.thisval.equals("-")) {
									visibilidad="private";
								}
								
								else if(s.thislabel==Token.Label.SIMBOLO && s.thisval.equals("+")){
									visibilidad="public";
									
								}
								else if(s.thislabel==Token.Label.SIMBOLO && s.thisval.equals("#")){
									visibilidad="protected";								
								}
								
								s = l.nextToken();
								
								if(s.thislabel==Token.Label.IDENTIFICADOR){
									
									nombre=(String)s.thisval;
									
									s = l.nextToken();
									
									if(s.thislabel==Token.Label.SIMBOLO && s.thisval.equals(":"))
									{
										System.out.println(":");
										
										s = l.nextToken();
										
										if (s.thislabel==Token.Label.RESERVADA && s.thisval.equals("String"))
										{
											
											tipo=(String)s.thisval;
											
											s = l.nextToken();
											
											if(s.thislabel==Token.Label.SIMBOLO && s.thisval.equals(")"))
											{
												
												System.out.println(")");
												
												clase.setAtributos(visibilidad, nombre, tipo);
												s = l.nextToken();
												
											}
											
											else
											{
												System.out.println("Error " + s.thisval);
											}
											
										}
										else
										{
											System.out.println("Error " + s.thisval);
										}
										
										
									}
									else{
										System.out.println("Error " + s.thisval);
									}
									
								}
								
								
								else{
									System.out.println("Error " + s.thisval);
								}
								
								
								
							}				
							
							
							
							else{
								
								System.out.println("Error " + s.thisval);
								
							}
							
							
							
							
							
						}
						else {
							System.out.println("Error " + s.thisval);
						}
						
					}
					
					
					//operaciones
					/*
					if (s.thislabel==Token.Label.RESERVADA && s.thisval.equals("attribute")) {
						System.out.println("attribute");
						s = l.nextToken();
					}
					else {
						
					}
					*/
					
				}
				
				
				
				else {
					System.out.println("Error " + s.thisval);					
				}
			}
			else{
				System.out.println("Error " + s.thisval);
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return null;
	}

	
	
	

}
